package com.cz.zslu.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.DateUtil;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.SysUser;
import com.cz.zslu.entity.ZslBanner;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.ISysUserService;
import com.cz.zslu.service.IZslBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 轮播管理 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/sys/banner")
@Api(description = "轮播管理")
public class BannerController extends BaseController<IZslBannerService, ZslBanner> {
    @Autowired
    private ISysUserService iSysUserService;


    @ApiOperation("获取本店铺的营销轮播")
    @RequestMapping(value = "/getMyBannerPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslBanner>> getMyBannerPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                         @ApiParam("页数") @RequestParam(defaultValue = "1") int offset) {
        SysUser user = iSysUserService.selectById(BaseContextHandler.getUserID());
        EntityWrapper<ZslBanner> ew = new EntityWrapper<>();
        ew.eq("user_id", BaseContextHandler.getUserID());
        ew.eq("type", 1);
        ew.orderBy("crt_time", false);
        Page<ZslBanner> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

    @ApiOperation("获取首页Banner展示")
    @IgnoreAuth
    @RequestMapping(value = "/getBanners", method = RequestMethod.POST)
    public ReturnObject<List<ZslBanner>> getBanners() {

        EntityWrapper<ZslBanner> ew = new EntityWrapper<>();
        ew.eq("type", 1);
        ew.andNew();
        ew.gt("start_time", new Date());
        ew.or().isNull("start_time");
        ew.andNew();
        ew.lt("end_time", new Date());
        ew.or().isNull("end_time");
        ew.orderBy("crt_time", false);
        return new ReturnObject<>(0, "获取成功", baseService.selectList(ew));
    }

    @ApiOperation("获取店铺Banner展示")
    @IgnoreAuth
    @RequestMapping(value = "/getBannersByStore", method = RequestMethod.POST)
    public ReturnObject<List<ZslBanner>> getBannersByStore(@ApiParam("店铺id") @RequestParam String storeId) {
        SysUser user = iSysUserService.selectOne(new EntityWrapper<SysUser>().eq("store_id", storeId));
        if (user.getType() != 2) {
            return new ReturnObject<>(0, "获取成功", null);
        }
        EntityWrapper<ZslBanner> ew = new EntityWrapper<>();
        ew.eq("type", 2);
        ew.eq("user_id", user.getId());
        ew.orderBy("crt_time", false);
        List<ZslBanner> list = baseService.selectList(ew);
        if (list.size() == 0) {
            return new ReturnObject<>(0, "获取成功", null);
        }
        return new ReturnObject<>(0, "获取成功", list);
    }


    @IgnoreAuth
    @ApiOperation("详情")
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public ReturnObject<ZslBanner> info(@ApiParam("id") Long id) {

        ZslBanner info = baseService.selectById(id);
        return new ReturnObject<>(0, "获取成功", info);
    }

    @ApiOperation("获取全部轮播分页")
    @RequestMapping(value = "/getBannerPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslBanner>> getBannerPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                       @ApiParam("页数") @RequestParam(defaultValue = "1") int offset) {
        EntityWrapper<ZslBanner> ew = new EntityWrapper<>();
        ew.eq("state", 0);
        ew.eq("type", 1);
        ew.orderBy("crt_time", false);
        Page<ZslBanner> obj = baseService.selectPage(new Page<>(offset, limit), ew);

        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ReturnObject<ZslBanner> add(@ApiParam("图片地址(必传)") @RequestParam String picUrl,
                                       @ApiParam("跳转链接") String link,
                                       @ApiParam("内容") String content,
                                       @ApiParam("轮播类型1:首页轮播，2：店铺轮播") Integer type,
                                       @ApiParam("开始时间(yyyy-MM-dd)") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                                       @ApiParam("结束时间(yyyy-MM-dd)") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
                                       @ApiParam("是否是营销推广（0：是，1：否）") Long marketing) {


        ZslBanner info = new ZslBanner();
        info.setState(0);
        info.setPicUrl(picUrl);
        info.setLink(link);
        info.setContent(content);
        info.setType(type);
        info.setUserId(BaseContextHandler.getUserID());
        info.setStartTime(startTime);
        info.setEndTime(endTime);
        if (marketing == 0) {
            info.setEndTime(DateUtil.getSomeDay(info.getStartTime(), 1));
            info.setType(1);
        }
        baseService.insert(info);
        return new ReturnObject<>(0, "保存成功", info);
    }


    @ApiOperation("编辑(营销轮播不可编辑)")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ReturnObject<ZslBanner> edit(@ApiParam("id") Long id, @ApiParam("图片地址(必传)") @RequestParam String picUrl,
                                        @ApiParam("跳转链接") String link, @ApiParam("内容") String content,
                                        @ApiParam("轮播类型1:首页轮播，2：店铺轮播") Integer type,
                                        @ApiParam("开始时间(yyyy-MM-dd)") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                                        @ApiParam("结束时间(yyyy-MM-dd)") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {

        ZslBanner info = baseService.selectById(id);
        info.setState(0);
        info.setPicUrl(picUrl);
        info.setLink(link);
        info.setContent(content);
        info.setType(type);
        info.setUserId(BaseContextHandler.getUserID());
        info.setStartTime(startTime);
        info.setEndTime(endTime);
        baseService.updateById(info);
        return new ReturnObject<>(0, "保存成功", info);
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ReturnObject<ZslBanner> add(@ApiParam("id") @RequestParam Long id) {

        baseService.deleteById(id);

        return new ReturnObject<>(0, "删除成功", null);
    }

}

